﻿using Shop.Collect;
using Shop.Model.ActivityGoods;
using Shop.Model.DB;
using Shop.RemoteModel;
using Shop.Service.Interface;
using WeDonekRpc.Helper;

namespace Shop.Service.Extend
{
    internal class ActivitySubmitAuditService : IActivityAuditService
    {
        private readonly IActivityCollect _Activity;
        private readonly IActivityDiscountCollect _Discount;
        private readonly IActivityGoodsCollect _ActivityGoods;
        private readonly IGoodsSkuStateCollect _SkuState;
        public ActivitySubmitAuditService (IActivityCollect activity,
            IActivityDiscountCollect discount,
            IGoodsSkuStateCollect skuState,
            IActivityGoodsCollect activityGoods)
        {
            this._SkuState = skuState;
            this._Activity = activity;
            this._Discount = discount;
            this._ActivityGoods = activityGoods;
        }

        public bool SubmitAudit (long activityId)
        {
            DBShopActivity activity = this._Activity.Get(activityId);
            if (activity.EndTime <= DateTime.Now)
            {
                throw new ErrorException("shop.activity.end.time.error");
            }
            if (activity.ActivityType == ActivityType.店铺促销)
            {
                this._CheckShop(activity);
            }
            else
            {
                this._CheckSingle(activity);
            }
            return this._Activity.SubmitAudit(activity);
        }
        /// <summary>
        /// 检查店铺促销
        /// </summary>
        /// <param name="activity"></param>
        /// <exception cref="ErrorException"></exception>
        private void _CheckShop (DBShopActivity activity)
        {
            if (activity.Range == DiscountRangeType.部分不参与 && !this._ActivityGoods.CheckIsExists(activity.Id))
            {
                throw new ErrorException("shop.activity.goods.null");
            }
            else if (this._Discount.CheckIsExists(activity.Id))
            {
                throw new ErrorException("shop.activity.discount.null");
            }
            else if (activity.Range == DiscountRangeType.部分参与)
            {
                long[] skuId = this._ActivityGoods.GetSkuIds(activity.Id);
                if (skuId.IsNull())
                {
                    throw new ErrorException("shop.activity.goods.null");
                }
                if (this._SkuState.CheckIsNoEnable(skuId))
                {
                    throw new ErrorException("shop.activity.goods.no.enable");
                }
            }
        }
        /// <summary>
        /// 检查单品促销
        /// </summary>
        /// <param name="activity"></param>
        /// <exception cref="ErrorException"></exception>
        private void _CheckSingle (DBShopActivity activity)
        {
            ActivityGoodsPrice[] list = this._ActivityGoods.GetsByActivityId<ActivityGoodsPrice>(activity.Id);
            if (list.IsNull())
            {
                throw new ErrorException("shop.activity.goods.null");
            }
            else if (list.IsExists(c => !c.ActivityPrice.HasValue || c.ActivityPrice.Value <= 0))
            {
                throw new ErrorException("shop.activity.goods.price.error");
            }
            else if (activity.ActivityType == ActivityType.套装促销 && list.Count(c => c.IsMust) < 2)
            {
                throw new ErrorException("shop.activity.goods.must.exceed.two");
            }
        }
    }
}
